package org.duncan.sjge.utils;

/**
 *
 * @author Duncan
 */
public class Vector2 {
    private double mX, mY;
    
    public final static double HALF_PI = Math.PI / 2;
    
    public Vector2() {
        this(0.0d, 0.0d);
    }
    
    public Vector2(double x, double y) {
        mX = x;
        mY = y;
    }
    
    public void setX(final double x) {mX = x;}
    public void setY(final double y) {mY = y;}
    public double getX() {return mX;}
    public double getY() {return mY;}
    
    public Vector2 add(final Vector2 other) {
        return new Vector2(
            mX + other.mX,
            mY + other.mY
        );
    }
    
    public Vector2 sub(final Vector2 other) {
        return new Vector2(
            mX - other.mX,
            mY - other.mY
        );
    }
    
    public Vector2 mult(final Vector2 other) {
        return new Vector2(
            mX * other.mX,
            mY * other.mY
        );
    }
    
    public Vector2 div(final Vector2 other) {
        return new Vector2(
            mX / other.mX,
            mY / other.mY
        );
    }
    
    public Vector2 add(final double other) {
        return new Vector2(
            mX + other,
            mY + other
        );
    }
    
    public Vector2 sub(final double other) {
        return new Vector2(
            mX - other,
            mY - other
        );
    }
    
    public Vector2 mult(final double other) {
        return new Vector2(
            mX * other,
            mY * other
        );
    }
    
    public Vector2 div(final double other) {
        return new Vector2(
            mX / other,
            mY / other
        );
    }
    
    public double dot(final Vector2 other) {
        return magn() * other.magn() * Math.cos(angle());
    }
    
    public double magn() {
        return Math.sqrt(mX * mX + mY * mY);
    }
    
    public Vector2 norm() {
        final double m = magn();
        return new Vector2(mX / m, mY / m);
    }
    
    public double angle() {
        if (mX == 0)
            if      (mY > 0) return HALF_PI;
            else if (mY < 0) return -HALF_PI;
            else             return 0;
        else return Math.atan(mY / mX);
    }
}
